package com.data.util;

/**
 * 天假信息到session中
 *
 * @author:HingLo
 * @create 2017-08-27 11:10
 **/

import com.data.bean.Account;
import com.data.dbdao.LogindbDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 根据传入的参数来添加到session中
 *
 * @author:HingLo
 * @create 2017-08-27 11:07
 **/
@Service
public class AddAccountUtils {
    @Autowired
    private LogindbDao logindbDao;

    /***
     * 返回session中的账号id
     * @param request
     * @return
     */

    public static String getAccountId(HttpServletRequest request) {
        Account account = (Account) request.getSession().getAttribute("Account");
        if (account != null) {
            return account.getId();
        }
        return null;
    }

    /**
     * 根据参数来添加登录的信息，可以是id,手机号，邮箱，账号
     *
     * @param request
     * @param params
     * @return
     */
    public Account addAccountSession(HttpServletRequest request, String params) {
        Account account = null;
        //参数是否为空或者空字符串
        if (CheckDataUtils.stringUtils(params)) {
            String type = LoginMethodUtils.accountType(params);
            account = logindbDao.findAccountDao(type, params);

        }
        request.getSession().setAttribute("Account", account);

        return account;
    }

    /**
     * 通过传递的参数查找账号
     *
     * @param params
     * @return
     */
    public Account getAccountSession(String params) {
        Account account = null;
        //参数是否为空或者空字符串
        if (CheckDataUtils.stringUtils(params)) {
            String type = LoginMethodUtils.accountType(params);
            account = logindbDao.findAccountDao(type, params);

        }
        return account;
    }

}
